import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'

// 密钥对生成 http://web.chacuo.net/netrsakeypair

const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuPX8sM1R+G/Y4t0rh6EMS7mNxe5NpdQ+tit7DcQ5Oazt9Z07gMXmDMN2DUvpz5G/jDw3n4pDmzqTpY8Xnk757tmHHtKdb5sujKvJ98Lmw9IhtzmhpiSWCgPcdePvgFT9G7GJgYm1aM7CCpU1tbd5dJGyG86/0spu0GzBEe3gRMQIDAQAB'

const privateKey = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAK49fywzVH4b9ji3SuHoQxLuY3F7k2l1D62K3sNxDk5rO31nTuAxeYMw3YNS+nPkb+MPDefikObOpOljxeeTvnu2Yce0p1vmy6Mq8n3wubD0iG3OaGmJJYKA9x14++AVP0bsYmBibVozsIKlTW1t3l0kbIbzr/Sym7QbMER7eBExAgMBAAECgYAdRvALeUSFPCWPOh03AFPk5nGHLACFA8pympb7uA9zVclVNPqn6ShFxvwxiMvUlVnHiUwI3qUCY06KeN1UIBstX2eAS3XANUyhXkkidWPa1GPigWoyphueXPdKsbCb1cPoDS/AvFtrUVP42tGYBzG498AG9+OG97VP9Jih8evBwQJBANbLk4lE8lRE/cQnyNMtPz/4gwyeSAgBTzK1TvCjoM4UT4wGvy0cZR+iiIQsghwER0YWT/dihM3CklLjKrPblo0CQQDPqkvn62TCTZTf5h9ALg4D0a+tr//VzVZZYJgcZnBqC35fJ1MwAyH5kbFA95DSmgMM5sAN0cmlhA3uacZ+qv41AkArQzWkIE7uyjaSm7DHWAW1TMiYcgjwE9Nv0zTjIxHwcAbVnUZEP8Pea04z0QMjwXjZlQTmPuGgz9/I8hhXwZLBAkBpBo63L4TZBBWsGx1o/yHVaqI37JAuGFh4gXLMwj5OU5vJEgFvgdYX+0gzPEHBUWUDjFxLYPNjw2KsVlCuVVqVAkEAv5K7SRvN+f65djf/svrRwUvbf5lq3ygDxZoN4IDuAhfecTtB3qO70GY5IYxteoUPORWL5t6OWHAVv6togxznWA=='

/**
 * RSA 加密
 * @param {带加密文本} txt 
 * @returns 
 */
export function encryptRSA(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(publicKey) // 设置公钥
  return encryptor.encrypt(txt) // 对数据进行加密
}

/**
 * RSA 解密
 * @param {待解密文本} txt 
 * @returns 
 */
export function decryptRSA(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey) // 设置私钥
  return encryptor.decrypt(txt) // 对数据进行解密
}


/**
 * AES 加解密相关
 */
import CryptoJS from "crypto-js";

var key = CryptoJS.enc.Utf8.parse("hanhancompleteme");
var iv = CryptoJS.enc.Utf8.parse("1993122319920119");

/**
 *  AES 加密
 * @param {待加密文本} txt 
 * @returns 
 */
export function encryptAES(txt) {
  var srcs = CryptoJS.enc.Utf8.parse(txt);
  var encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.toString();
}

/**
 * AES 解密
 * @param {待解密文本} txt 
 * @returns 
 */
export function decryptAES(txt) {
  var decrypted = CryptoJS.AES.decrypt(txt, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return decrypted.toString(CryptoJS.enc.Utf8);
}